<?php
class ProfileController extends Controller {
	
	public function indexAction() {
        $this->checkLoginRedirect();
        $user = $this->_session->getCurrent();
		$currSettings = new UserSettings($user->id);
		
		$dicts = $this->_model->getAllDict();
		$langs = FileSystem::scanFsForInstalledLangs();

		$this->_view->assignArray(array(
            'user' => $user,
			'pageTitle' => 'Localization system :: User profile',
			'dicts' => $dicts,
			'langs' => $langs,
			'currLanguage' => $currSettings->getLanguage(),
			'currLeksPerPage' => $currSettings->getLeksPerPage(),
			'currDict' => $currSettings->getPreferDict(),
            'pageId' => 'profile'
		));
		$this->_view->render('profile/index');
	}

	public function savesettingsAction() {
		$user = $this->_session->getCurrent();
		$systemLanguage = $this->_request->getString('sysLanguage');
		$prefDict = $this->_request->getInt('prefDict');
		$leksPerPage = $this->_request->getInt('leksPerPage');
		$settings = new UserSettings($user->id);
		$settings->setLanguage(($systemLanguage === -1) ? 'english' : $systemLanguage);
		$settings->setLeksPerPage((empty($leksPerPage)) ? Config::$defaultPerPage : $leksPerPage);
		$settings->setPreferDict(($prefDict === -1) ? 0 : $prefDict);
		$settings->save();
		$this->redirect('profile');
	}
}